﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xyz.Core.Data;
using Xyz.Core.Infrastructure;
using Xyz.Core.Interfaces;
using Xyz.Core.Models;
using System.Linq;
using Xyz.Core.Models.Filters;
using Xyz.Core.Infrastructure.Extentions;

namespace Xyz.Core.Services.AppServices
{
    public class TypeserverService : ITypeserverService
    {
        private readonly FlightContext _context;
        private readonly IMapper _mapper;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="context"></param>
        /// <param name="mapper"></param>
        public TypeserverService(FlightContext context, IMapper mapper)
        {
            _mapper = mapper;
            _context = context;
        }

        public async Task<PagedResult<TypeserverDto>> SearchAsync(TypeserverFilters filters)
        {
            if (filters == null)
                return new PagedResult<TypeserverDto>(1, 0);
            // var query = _context.Hbxxs.Where(item => !item.IsDeleted);
            var query = from s in _context.Typeservers
                        select s;

            if (filters.keywords.IsNotBlank())
                query = query.Where(s => s.Types.Contains(filters.keywords));

            //return view();
            return await query.OrderByDescending(item => item.Types)
              .Select(item => new TypeserverDto
              {
                  Id = item.Id,
                  Types = item.Types
              }).PagingAsync(filters.page, filters.rows);
        }
    }
}
